// pages/my/change_detail.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hidden:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var that = this
    // —————————————— 获取网址——————————
    app.util.request({
      "url": "entry/wxapp/Attachurl",
      'cachetime': '0',
      success: function (res) {
        that.setData({
          url: res.data
        })
      }
    })
    var prize_id = options.prize_id
    that.setData({
      prize_id: prize_id
    })
    app.util.request({
      'url': 'entry/wxapp/PrizeDetails',
      'cachetime': '0',
      data: {
        prize_id: prize_id,
      },
      success: function (res) {
        console.log("积分兑换详情")
        console.log(res.data)
        if (res.data.code == 200) {
          that.setData({
            list: res.data.list,
            descript: res.data.list.description,
            need_score: res.data.list.need_score
          })
        }
        that.setData({
          none: res.data,
        })
      },
    })

    var user_id = wx.getStorageSync('user_msg').id
    //获取总积分
    app.util.request({
      "url": "entry/wxapp/MyScores",
      'cachetime': '0',
      data: {
        user_id: user_id,
      },
      success: function (res) {
        console.log(res)
        if (res.data.code == 200) {
          that.setData({
            score: res.data.list.score
          })
        }
      }
    })

},

//点击兑换
  onTap:function(e){
    var that=this;
    var userid = wx.getStorageSync('user_msg').id
    var user_name = wx.getStorageSync('user_msg').students_name
    var phone = wx.getStorageSync('user_msg').phone_number   
    var all=that.data.list
    console.log(all)
    var prize_id = all.id
    var score = all.need_score
    var num=all.num
    var prize_name=all.name
    var prize_img = all.pic
    //总积分
    var score = Number(that.data.score)
    //所需积分
    var need_score = Number(that.data.need_score)
    //console.log(score)
    //console.log(need_score)
    
    if (score < need_score){
      wx.showToast({
        title: "积分不足",
        icon: "none",
        duration: 1000
      }) 
    }else{
      app.util.request({
        'url': 'entry/wxapp/CheckLq',
        'cachetime': '0',
        data: {
          prize_id: prize_id,
          user_id: userid,
        },
        success: function (res) {
          console.log("兑换成功")
          console.log(res)
          console.log(prize_id)
          that.setData({
            bath: res.data
          })
          if (res.data.code == 501) {
            wx.showToast({
              title: "兑换失败，不能重复兑换该奖品",
              icon: "none",
              duration: 1000
            })
          }
        },
      })
    }
  },

  /*核销二维码 */
  // onCode:function(e){
  //   wx.scanCode({
  //     onlyFromCamera:false,
  //     success:function(res){
  //       console.log(res)
  //     }
  //   })
  // },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})